package snmp;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;



public class SnmpScript {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
			

			
			
			//---------------------------------------------------------------------------------------------------------------				
			//defining target and port
			Address targetAddress = GenericAddress.parse("udp:127.0.0.2/161");
			TransportMapping transport = new DefaultUdpTransportMapping();
			Snmp snmp = new Snmp(transport);
			USM usm = new USM(SecurityProtocols.getInstance(),new OctetString(MPv3.createLocalEngineID()), 0);
			SecurityModels.getInstance().addSecurityModel(usm);
			transport.listen();
			//---------------------------------------------------------------------------------------------------------------			
			// setting up target
			CommunityTarget target = new CommunityTarget();
			target.setCommunity(new OctetString("public"));
			target.setAddress(targetAddress);
			target.setRetries(2);
			target.setTimeout(1500);
			target.setVersion(SnmpConstants.version2c);
			// creating PDU
			PDU pdu = new PDU();
			pdu.add(new VariableBinding(new OID(new int[]{1,3,6})));
			
			pdu.setType(PDU.GETNEXT);
			

			
			 int cont=0;
			while(true){
				pdu = snmp.getNext(pdu, target).getResponse();
				System.out.println("OID:  "+pdu.get(0).getOid()+" Value:  "+pdu.get(0).getVariable());
				if(pdu.get(0).getVariable().toString().contains("endOfMibView"))
					break;
				cont++;
				
				
			}
			//radical for blocking 
			
			
			
	}


}